package com.taotao.manage.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.utils.ItemCatResult;
import com.taotao.manage.pojo.ItemCat;
import com.taotao.manage.service.ItemCatService;
/**
 * 查询商品类目数据。
 * @author 铭
 *
 */
@RequestMapping("item/cat")
@Controller
public class ItemCatController {
	
	@Autowired
	private ItemCatService itemCatService;
	
	private static final ObjectMapper MAPPER = new ObjectMapper();
	private static final Logger LOGGER = LoggerFactory.getLogger(ItemCatController.class);
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<List<ItemCat>> queryItemCatList(@RequestParam(value="id",defaultValue="0")Long parentId) {
		try {
			ItemCat itemCat = new ItemCat();
			itemCat.setParentId(parentId);
			List<ItemCat> list =this.itemCatService.queryByWhere(itemCat);
			if (list==null||list.size()==0) {
				//资源不存在，返回404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
				return ResponseEntity.ok(list);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
	}
	/**
	 * 按前台需求数据结构，返回数据
	 * @return
	 */
	@RequestMapping(value="all",method=RequestMethod.GET)
	public ResponseEntity<ItemCatResult> queryItemCatAll() {
		ItemCatResult result = itemCatService.queryAllToTree();
		try {
			if (result!=null) {
				return ResponseEntity.status(HttpStatus.OK).body(result);
			}else {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		
	}
	
	/**
	 * 查询商品类目名字
	 * @param id
	 * @return
	 */
	@RequestMapping(value="ItemCatName",method=RequestMethod.GET)
	public ResponseEntity<ItemCat> queryItemCatById(@RequestParam("id") Long id) {
		try {
			ItemCat itemCat = new ItemCat();
			itemCat.setId(id);
			ItemCat itemCat2 =this.itemCatService.queryOne(itemCat);
			if (itemCat2==null) {
				//资源不存在，返回404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
				return ResponseEntity.ok(itemCat2);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
	}
}
